//
//  JSTNotification.m
//  KYKJPackDemo
//
//  Created by 01 on 16/12/5.
//  Copyright © 2016年 蔡正军. All rights reserved.
//

#import "JSTNotification.h"

@implementation JSTNotification

+ (void)createNotification
{
    [JSTServer requestNotification:^(NSDictionary *notifiData) {
    
        if (IsArrEmpty(notifiData)) return;
        NSString *statusStr = [NSString stringWithFormat:@"%@", [notifiData valueForKey:@"status"]];
        if (statusStr.integerValue <= 0) return;
        NSArray *fmData = [JSTFMDBManager querySqlforTab:[JSTPushData modelWith:nil title:nil content:nil link:nil pushTime:nil]];
        for (int i = 0; i < fmData.count; i++) {
            JSTPushData *model = fmData[i];
            if ([model.pushId isEqualToString:[notifiData valueForKey:@"id"]]) return;
        }
        JSTPushData *pushModel              = [[JSTPushData alloc] init];
        pushModel.pushId                    = kStr([notifiData valueForKey:@"id"]);
        pushModel.link                      = kStr([notifiData valueForKey:@"link"]);
        pushModel.title                     = kStr([notifiData valueForKey:@"title"]);
        pushModel.content                   = kStr([notifiData valueForKey:@"content"]);
        pushModel.pushTime                  = kStr([notifiData valueForKey:@"push_time"]);
        [JSTFMDBManager insertOrUpdateModelToDatabase:pushModel];
        DLog(@"---- notifiData = %@", notifiData);
        long currentTime = time(NULL);
        long push_time = [[notifiData valueForKey:@"push_time"] longValue];
        long interval = 1;
        if (push_time > currentTime) {
            interval = push_time - currentTime;
        }
        // 创建一个本地推送
        UILocalNotification *notification = [[UILocalNotification alloc] init];
        //设置10秒之后
        NSDate *pushDate = [NSDate dateWithTimeIntervalSinceNow:interval];
        if (notification != nil) {
            // 设置推送时间
            notification.fireDate = pushDate;
            // 设置时区
            notification.timeZone = [NSTimeZone defaultTimeZone];
            // 设置重复间隔
//            notification.repeatInterval = kCFCalendarUnitDay;
            // 推送声音
            notification.soundName = UILocalNotificationDefaultSoundName;
            // 推送内容
            notification.alertBody = @"推送内容";
            //显示在icon上的红色圈中的数子
            notification.applicationIconBadgeNumber++;
            //设置userinfo 方便在之后需要撤销的时候使用
            notification.userInfo = [NSDictionary dictionaryWithObject:notifiData forKey:@"notificationKey"];
            //添加推送到UIApplication
            UIApplication *app = [UIApplication sharedApplication];
            [app scheduleLocalNotification:notification];
        }
    }];
}

+ (void)cancelNotification
{
    // 获取所有本地通知数组
    NSArray *localNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;
    
    for (UILocalNotification *notification in localNotifications) {
        NSDictionary *userInfo = notification.userInfo;
        if (userInfo) {
            // 根据设置通知参数时指定的key来获取通知参数
            NSString *info = userInfo[@"key"];
            // 如果找到需要取消的通知，则取消
            if (info != nil) {
                [[UIApplication sharedApplication] cancelLocalNotification:notification];
                break;  
            }  
        }  
    }
}

@end
